package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.admin.AdminRoleDTO;
import com.etone.smartAudit.dto.adminMenu.MenuPermissionDTO;
import com.etone.smartAudit.service.AdminMenuService;
import com.etone.smartAudit.service.AdminRoleService;
import com.etone.smartAudit.service.PermissionService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.adminRole.AdminRoleListVO;
import com.etone.smartAudit.vo.adminRole.AdminRoleVO;
import com.etone.smartAudit.vo.menu.MenuVO;
import com.etone.smartAudit.vo.permission.PermissionSimpleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-06-07 18:05:56
 * @Description
 */
@AllArgsConstructor
@RestController
@Api(tags = "角色管理", value = "AdminRoleController")
public class AdminRoleController {
    AdminRoleService adminRoleService;

    PermissionService permissionService;

    AdminMenuService adminMenuService;

    @ApiOperation(value = "角色列表《完》")
    @GetMapping("/adminRoles")
    public PagerVO<AdminRoleListVO> list(
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "角色名称") @RequestParam(required = false) String name
    ) {
        return adminRoleService.list(name, page, pageSize);
    }

    @ApiOperation(value = "角色列表《完》")
    @GetMapping("/adminRoles/{id}")
    public AdminRoleVO detail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return adminRoleService.detail(id);
    }

    @AdminOperation(module = "角色管理", item = "新增")
    @ApiOperation(value = "增加角色《完》")
    @PostMapping("/adminRoles")
    public void add(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "表单详情") @RequestBody @Validated AdminRoleDTO adminRoleDTO
    ) {
        adminRoleService.save(adminRoleDTO, null, admin);
    }

    @AdminOperation(module = "角色管理", item = "编辑")
    @ApiOperation(value = "编辑角色《完》")
    @PutMapping("/adminRoles/{id}")
    public void edit(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "表单详情") @RequestBody @Validated AdminRoleDTO adminRoleDTO
    ) {
        adminRoleService.save(adminRoleDTO, id, admin);
    }

    @ApiOperation(value = "删除角色《完》")
    @DeleteMapping("/adminRoles/{id}")
    @AdminOperation(module = "角色管理", item = "删除")
    public void delete(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        adminRoleService.delete(id, true);
    }

    @ApiOperation(value = "批量删除角色《完》")
    @DeleteMapping("/adminRoles/batch/{ids}")
    @AdminOperation(module = "角色管理", item = "批量删除")
    public void delete(
            @ApiParam(value = "ids") @PathVariable List<Integer> ids
    ) {
        adminRoleService.delete(ids, true);
    }

    @ApiOperation(value = "角色权限列表《完》")
    @GetMapping("/adminRoles/{id}/permissions")
    public PagerVO<PermissionSimpleVO> rolePermissions(
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return permissionService.findSimpleByRoleId(page, pageSize, id);
    }

    @ApiOperation(value = "修改管理员权限《完》")
    @PutMapping("/adminRoles/{id}/permissions")
    public void updateRolePermissions(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "权限id") @RequestBody List<MenuPermissionDTO> menumPDto
    ) {
        adminRoleService.updateRolePermissions(id, menumPDto);
    }

    @ApiOperation(value = "角色菜单列表《完》")
    @GetMapping("/adminRoles/{id}/menus")
    public List<MenuVO> roleMenus(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return adminMenuService.findRoleMenus(id);
    }

    @ApiOperation(value = "修改管理员菜单《完》")
    @PutMapping("/adminRoles/{id}/menus")
    public void updateRoleMenus(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "菜单id") @RequestBody List<Integer> menuIds
    ) {
        adminRoleService.updateRoleMenus(id, menuIds);
    }

}
